// 切换登录和注册表单
function switchForm(type) {
    if (type === 'login') {
        document.getElementById('loginForm').style.display = 'block';
        document.getElementById('registerForm').style.display = 'none';
    } else {
        document.getElementById('loginForm').style.display = 'none';
        document.getElementById('registerForm').style.display = 'block';
    }
    // 清除所有错误信息
    clearErrors();
}

// 清除错误信息
function clearErrors() {
    const errorElements = document.getElementsByClassName('error-message');
    const inputs = document.getElementsByTagName('input');
    const icons = document.getElementsByClassName('icon');
    
    for (let element of errorElements) {
        element.style.display = 'none';
        element.textContent = '';
    }
    
    // 清除输入框和图标的错误状态
    for (let input of inputs) {
        input.classList.remove('error');
    }
    for (let icon of icons) {
        icon.classList.remove('error');
    }
}

// 显示错误信息
function showError(elementId, message) {
    const errorElement = document.getElementById(elementId);
    const input = errorElement.parentElement.querySelector('input');
    const icon = errorElement.parentElement.querySelector('.icon');
    
    errorElement.textContent = message;
    errorElement.style.display = 'block';
    
    // 添加错误状态样式
    if (input) {
        input.classList.add('error');
    }
    if (icon) {
        icon.classList.add('error');
    }
}

// 验证用户名
function validateUsername(username) {
    if (!username) {
        return '用户名不能为空';
    }
    if (username.length < 3) {
        return '用户名长度不能小于3个字符';
    }
    return '';
}

// 验证密码
function validatePassword(password) {
    if (!password) {
        return '密码不能为空';
    }
    if (password.length < 6) {
        return '密码长度不能小于6个字符';
    }
    return '';
}

// 处理登录
function handleLogin() {
    const username = document.getElementById('loginUsername').value;
    const password = document.getElementById('loginPassword').value;
    
    clearErrors();
    
    let hasError = false;
    const usernameError = validateUsername(username);
    const passwordError = validatePassword(password);
    
    if (usernameError) {
        showError('loginUsernameError', usernameError);
        hasError = true;
    }
    
    if (passwordError) {
        showError('loginPasswordError', passwordError);
        hasError = true;
    }
    
    if (hasError) {
        return;
    }
    
    // TODO: 这里添加实际的登录逻辑
    alert('登录成功！');
}

// 处理注册
function handleRegister() {
    const username = document.getElementById('registerUsername').value;
    const password = document.getElementById('registerPassword').value;
    const confirmPassword = document.getElementById('confirmPassword').value;
    
    clearErrors();
    
    let hasError = false;
    const usernameError = validateUsername(username);
    const passwordError = validatePassword(password);
    
    if (usernameError) {
        showError('registerUsernameError', usernameError);
        hasError = true;
    }
    
    if (passwordError) {
        showError('registerPasswordError', passwordError);
        hasError = true;
    }
    
    if (password !== confirmPassword) {
        showError('confirmPasswordError', '两次输入的密码不一致');
        hasError = true;
    }
    
    if (hasError) {
        return;
    }
    
    // TODO: 这里添加实际的注册逻辑
    alert('注册成功！');
    // 注册成功后跳转到登录页
    window.location.href = 'index.html';
} 